#include "widget.h"
#include "ui_widget.h"

int Widget::currentIndex = 0;
int Widget::cnt = 0;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("企业人事管理系统");
    this->resize(1600,800);

    //给单选和多选添加组：
    QButtonGroup* sex = new QButtonGroup;
    sex->addButton(ui->nan);
    sex->addButton(ui->nv);
    sex->addButton(ui->gay);

    QButtonGroup* major = new QButtonGroup;
    major->addButton(ui->c);
    major->addButton(ui->cpp);
    major->addButton(ui->datastructure);
    major->addButton(ui->mfc);
    major->addButton(ui->mysql);
    major->setExclusive(false);     //设置不互斥

    //给复选框添加项目：
    ui->minzu->addItem("汉族");
    ui->minzu->addItem("蒙古族");
    ui->minzu->addItem("藏族");
    ui->minzu->addItem("清真");
    ui->minzu->addItem("苗族");
    ui->minzu->addItem("维吾尔族");
    ui->minzu->setCurrentIndex(0);

    //设置出生日期默认日期：
    ui->chusheng->setDate(QDate::currentDate());

    //给tab加项目：
    ui->tab->setColumnCount(9);
    ui->tab->setHorizontalHeaderLabels(QStringList()<<"学号"<<"姓名"<<"性别"<<"年龄"<<"身高"<<"体重"<<"民族"<<"出生日期"<<"技能掌握");
    ui->tab->setEditTriggers(QAbstractItemView::NoEditTriggers);        //设置不可编辑
    ui->tab->setSelectionBehavior(QAbstractItemView::SelectRows);       //设置整行选取
    ui->tab->setSelectionMode(QAbstractItemView::SingleSelection);      //设置单行选中


    //双击tab行，把tab的数据添加到上面的edit中：
    connect(ui->tab,SIGNAL(itemDoubleClicked(QTableWidgetItem *)),this,SLOT(on_DoubleTabitem_clicked()));

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_addButton_clicked()
{
#if 1
    //方式1：使用全局变量；
#else
    //方式2：使用函数获取当前的行号：
    int currentIndex = ui->tab->rowCount();
#endif
    //获取各种数据添加到tab中：
    ui->tab->insertRow(currentIndex);
    //学号：
    ui->tab->setItem(currentIndex,0,new QTableWidgetItem(ui->xuehao_edit->text()));
    //姓名：
    ui->tab->setItem(currentIndex,1,new QTableWidgetItem(ui->name_edit->text()));
    //性别：
    QString strsex = ui->nan->isChecked()?ui->nan->text():(ui->nv->isChecked()?ui->nv->text():ui->gay->text());
    ui->tab->setItem(currentIndex,2,new QTableWidgetItem(strsex));
    //年龄：
    ui->tab->setItem(currentIndex,3,new QTableWidgetItem(QString("%1").arg(ui->age->value())));
    //身高：
    ui->tab->setItem(currentIndex,4,new QTableWidgetItem(ui->shengao->text()));
    //体重：
    ui->tab->setItem(currentIndex,5,new QTableWidgetItem(ui->tizhong->text()));
    //民族：
    ui->tab->setItem(currentIndex,6,new QTableWidgetItem(ui->minzu->currentText()));
    //出生日期 ：
    ui->tab->setItem(currentIndex,7,new QTableWidgetItem(ui->chusheng->date().toString("yyyy-MM-dd")));
    //技能掌握：
    QString str;
    if(ui->c->isChecked())
    {
        str += ui->c->text() + ";";
    }
    if(ui->cpp->isChecked())
    {
        str += ui->cpp->text() + ";";
    }
    if(ui->mfc->isChecked())
    {
        str += ui->mfc->text() + ";";
    }
    if(ui->mysql->isChecked())
    {
        str += ui->mysql->text() + ";";
    }
    if(ui->datastructure->isChecked())
    {
        str += ui->datastructure->text() + ";";
    }
    ui->tab->setItem(currentIndex,8,new QTableWidgetItem(str));
    currentIndex++;



}

void Widget::on_delButton_clicked()
{
    //找出当前的行号：
    int sel = ui->tab->currentRow();
    if(sel <0)
    {
        qDebug()<<"nothing";
        return;
    }

    ui->tab->removeRow(sel);
}

void Widget::on_modifyButton_clicked()
{
    //找出当前的行号：
    int sel = ui->tab->currentRow();
    if(sel <0)
    {
        qDebug()<<"nothing";
        return;
    }
    /*
    //学号：
    ui->xuehao_edit->setText(ui->tab->item(sel,0)->text());
    //姓名：
    ui->name_edit->setText(ui->tab->item(sel,1)->text());
    //性别：
    if(ui->tab->item(sel,2)->text() == ui->nan->text())
    {
        ui->nan->setChecked(true);
    }
    else if(ui->tab->item(sel,2)->text() == ui->nv->text())
    {
        ui->nv->setChecked(true);
    }
    else
    {
        ui->gay->setChecked(true);
    }
    //年龄：
    ui->age->setValue(ui->tab->item(sel,3)->text().toInt());
    //身高：
    ui->shengao->setText(ui->tab->item(sel,4)->text());
    //体重：
    ui->tizhong->setText(ui->tab->item(sel,5)->text());
    //民族：
    ui->minzu->setCurrentText(ui->tab->item(sel,6)->text());
    //出生日期：
    QDate date = QDate::fromString(ui->tab->item(sel,7)->text(),"yyyy-MM-dd");
    ui->chusheng->setDate(date);
    //技能：
        //先把复选框清空：
    ui->c->setChecked(false);
    ui->cpp->setChecked(false);
    ui->mfc->setChecked(false);
    ui->datastructure->setChecked(false);
    ui->mysql->setChecked(false);
        //赋值：
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->c->text()))
    {
        ui->c->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->cpp->text()))
    {
        ui->cpp->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->mfc->text()))
    {
        ui->mfc->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->datastructure->text()))
    {
        ui->datastructure->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->mysql->text()))
    {
        ui->mysql->setChecked(true);
    }
    */

    //获取各种数据添加到tab中：
    //学号：
    ui->tab->setItem(sel,0,new QTableWidgetItem(ui->xuehao_edit->text()));
    //姓名：
    ui->tab->setItem(sel,1,new QTableWidgetItem(ui->name_edit->text()));
    //性别：
    QString strsex = ui->nan->isChecked()?ui->nan->text():(ui->nv->isChecked()?ui->nv->text():ui->gay->text());
    ui->tab->setItem(sel,2,new QTableWidgetItem(strsex));
    //年龄：
    ui->tab->setItem(sel,3,new QTableWidgetItem(QString("%1").arg(ui->age->value())));
    //身高：
    ui->tab->setItem(sel,4,new QTableWidgetItem(ui->shengao->text()));
    //体重：
    ui->tab->setItem(sel,5,new QTableWidgetItem(ui->tizhong->text()));
    //民族：
    ui->tab->setItem(sel,6,new QTableWidgetItem(ui->minzu->currentText()));
    //出生日期 ：
    ui->tab->setItem(sel,7,new QTableWidgetItem(ui->chusheng->date().toString("yyyy-MM-dd")));
    //技能掌握：
    QString str;
    if(ui->c->isChecked())
    {
        str += ui->c->text() + ";";
    }
    if(ui->cpp->isChecked())
    {
        str += ui->cpp->text() + ";";
    }
    if(ui->mfc->isChecked())
    {
        str += ui->mfc->text() + ";";
    }
    if(ui->mysql->isChecked())
    {
        str += ui->mysql->text() + ";";
    }
    if(ui->datastructure->isChecked())
    {
        str += ui->datastructure->text() + ";";
    }
    ui->tab->setItem(sel,8,new QTableWidgetItem(str));

}

void Widget::on_DoubleTabitem_clicked()
{
    //找出当前的行号：
    int sel = ui->tab->currentRow();
    if(sel <0)
    {
        qDebug()<<"nothing";
        return;
    }

    //学号：
    ui->xuehao_edit->setText(ui->tab->item(sel,0)->text());
    //姓名：
    ui->name_edit->setText(ui->tab->item(sel,1)->text());
    //性别：
    if(ui->tab->item(sel,2)->text() == ui->nan->text())
    {
        ui->nan->setChecked(true);
    }
    else if(ui->tab->item(sel,2)->text() == ui->nv->text())
    {
        ui->nv->setChecked(true);
    }
    else
    {
        ui->gay->setChecked(true);
    }
    //年龄：
    ui->age->setValue(ui->tab->item(sel,3)->text().toInt());
    //身高：
    ui->shengao->setText(ui->tab->item(sel,4)->text());
    //体重：
    ui->tizhong->setText(ui->tab->item(sel,5)->text());
    //民族：
    ui->minzu->setCurrentText(ui->tab->item(sel,6)->text());
    //出生日期：
    QDate date = QDate::fromString(ui->tab->item(sel,7)->text(),"yyyy-MM-dd");
    ui->chusheng->setDate(date);
    //技能：
        //先把复选框清空：
    ui->c->setChecked(false);
    ui->cpp->setChecked(false);
    ui->mfc->setChecked(false);
    ui->datastructure->setChecked(false);
    ui->mysql->setChecked(false);
        //赋值：
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->c->text()))
    {
        ui->c->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->cpp->text()))
    {
        ui->cpp->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->mfc->text()))
    {
        ui->mfc->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->datastructure->text()))
    {
        ui->datastructure->setChecked(true);
    }
    if(-1 != ui->tab->item(sel,8)->text().indexOf(ui->mysql->text()))
    {
        ui->mysql->setChecked(true);
    }



}

bool Widget::event(QEvent *ev)
{
    return QWidget::event(ev);
}

void Widget::keyPressEvent(QKeyEvent *ev)
{
    switch(ev->key())
    {
        case Qt::Key_Return:
        on_addButton_clicked();
        break;
    }

}
